package com.keyidea.jfxui.core.config.db;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * 第二数据库，sqlite
 */
@Configuration
// 配置mapper路径
@MapperScan(basePackages = "com.keyidea.**.sqlite.mapper", sqlSessionFactoryRef = "sqliteSqlSessionFactory")
public class SqliteDataSourceConfig {

    @Primary
    // 将这个对象放入Spring容器中
    @Bean(name = "sqliteDataSource")
    // prefix表示参数的前缀, 读取application.properties中的配置参数映射成为一个对象
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.sqlite")
    public DataSource getDateSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "sqliteSqlSessionFactory")
    // @Qualifier表示查找Spring容器中名字为sqliteDataSource的对象
    public SqlSessionFactory sqliteSqlSessionFactory(@Qualifier("sqliteDataSource") DataSource datasource)
            throws Exception {
        // mybatis定义的sqlsession
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        // 插件对象
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 分页插件
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQLITE));
        bean.setPlugins(mybatisPlusInterceptor);

        // mybatis配置对象
        MybatisConfiguration config = new MybatisConfiguration();
        // 开启javaBean 成员变量名映射为映射到数据库列名的时候，驼峰命名法到下划线分割命名法则的自动转换功能（）
        config.setMapUnderscoreToCamelCase(true);
        //增加对mybatis自带分页的支持
        config.setCacheEnabled(false);
        bean.setConfiguration(config);

        // 全局配置对象
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setBanner(false);
        bean.setGlobalConfig(globalConfig);

        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:*/sqlitemapper/*.xml"));
        bean.setTypeAliasesPackage("com.keyidea.**.sqlite.entity.**");
        return bean.getObject();
    }

    @Primary
    @Bean("sqliteSqlSessionTemplate")
    public SqlSessionTemplate sqliteSqlSessionTemplate(
            @Qualifier("sqliteSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

    @Primary
    @Bean("sqliteTransactionManager")
    public PlatformTransactionManager sqliteTransactionManager(@Qualifier("sqliteDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
